﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TSSControlLibrary
{
    public partial class SetCronControl : UserControl
    {
        public delegate void Vaction(string cronExpression);
        public Vaction ReturnCronExpression;
        private CronDataNodel dataCron = new CronDataNodel();
        /// <summary>
        /// 
        /// </summary>
        public SetCronControl()
        {
            InitializeComponent();
            InitializeData();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void InitializeData()
        {
            this.cronSecondControl1.returnExpression = dealExpression;
            this.cronMinuteControl1.returnExpression = dealExpression;
            this.cronHourControl1.returnExpression = dealExpression;
            this.cronDayControl1.returnExpression = dealExpression;
            this.cronMonthControl1.returnExpression = dealExpression;
            this.cronWeekControl1.returnExpression = dealExpression;
            this.cronYearControl1.returnExpression = dealExpression;
            this.cronDisplayControl1.OpCronExpression = ReturnValidCronExpression;
        }
        /// <summary>
        /// 处理传回的表达式
        /// </summary>
        /// <param name="Expression"></param>
        /// <param name="c"></param>
        private void dealExpression(string Expression, Control c)
        {
            if (c is CronSecondControl)
            {
                dataCron.secondExpression = Expression + " ";
            }
            else if(c is CronMinuteControl)
            {
                dataCron.minuteExpression = Expression + " ";
            }
            else if (c is CronHourControl)
            {
                dataCron.hourExpression = Expression + " ";
            }
            else if (c is CronDayControl)
            {
                dataCron.dayExpression = Expression + " ";
            }
            else if (c is CronMonthControl)
            {
                dataCron.monthExpression = Expression + " ";
            }
            else if (c is CronWeekControl)
            {
                dataCron.weekExpression = Expression + " ";
            }
            else if (c is CronYearControl)
            {
                dataCron.yearExpresiion = Expression;
            }
            this.cronDisplayControl1.DealExpression(dataCron, c);
        }
        /// <summary>
        /// 将用户已保存的有效cron表达式传回任务创建主界面
        /// </summary>
        /// <param name="validCron"></param>
        private void ReturnValidCronExpression(string validCron)
        {
            ReturnCronExpression(validCron);
        }
    }
    /// <summary>
    /// cron数据类
    /// </summary>
    public class CronDataNodel
    {
        public string secondExpression = "* ";
        public string minuteExpression = "* ";
        public string hourExpression = "* ";
        public string dayExpression = "* ";
        public string monthExpression = "* ";
        public string weekExpression = "? ";
        public string yearExpresiion = "*";
    }
}
